<template>
  <div v-show="show" class="to-top-container" @click="handleClcik">Top</div>
</template>

<script>
export default {
  name: 'ToTop',
  data() {
    return {
      show: false,
    }
  },
  created() {
    this.$bus.$on('mainScroll', this.handleScroll)
  },
  destroyed() {
    this.$bus.$off('mainScroll', this.handleClcik)
  },
  methods: {
    handleScroll(dom) {
      if (!dom) {
        this.show = false
        return
      }
      this.show = dom.scrollTop >= 500
    },
    handleClcik() {
      this.$bus.$emit('setMainScroll', 0)
    },
  },
}
</script>

<style scoped lang="less">
@import '~@/styles/var.less';

.to-top-container {
  background-color: @primary;
  width: 50px;
  height: 50px;
  line-height: 50px;
  border-radius: 50%;
  position: fixed;
  z-index: 99;
  right: 50px;
  bottom: 50px;
  cursor: pointer;
  color: #fff;
  text-align: center;
}
</style>
